<?php

    $rootDir = $_SERVER['DOCUMENT_ROOT'];

    require_once($rootDir.'/classes/db/SQLiteConnector.class.php');   
    require_once($rootDir.'/classes/Card.class.php');
    require_once($rootDir.'/classes/LeproUser.class.php');   
    require_once($rootDir.'/classes/db/LeproUserDAO.class.php');
    require_once($rootDir.'/classes/LeproBJGroup.class.php');   
    require_once($rootDir.'/classes/db/LeproBJGroupDAO.class.php');
    require_once($rootDir.'/functions.php');
    
    $infoType = urldecode($_POST['mode']);
    $infoValue = urldecode($_POST['value']);       
    
    if (preg_match("/^(username|user|group)$/", $infoType) > 0) {
        $connector = SQLiteConnector::getInstance();
        if ($connector->connect()) { // connect to DB
            $userDAO = new LeproUserDAO($connector->getDB());
            $groupDAO = new LeproBJGroupDAO($connector->getDB());
            if ($infoType == 'username') {                                
                if (preg_match("/^[A-Za-z0-9_.-]+$/", $infoValue) > 0) {
                    $infoValue = $userDAO->getUserId($infoValue);
                    $infoType = 'user';
                } else echo ':(';
            }
            switch($infoType) {
                case 'user': {
                        if (preg_match("/^\d+$/", $infoValue) > 0) {                        
                            $user = $userDAO->load($infoValue);
	                        $userPos = $userDAO->getUserPos($infoValue);
	                        if ($user != null) {
	                            $userInfo = $user->getSummary();
	                            //var_dump($groupInfo); 
	                            $userInfo['uid'] = userIdConvertor($userInfo['uid']);
	                            $userInfo['rtime'] = format_time($userInfo['rtime']);
	                            $userInfo['card'] = Card::getCardSummary($userInfo['card']);
	                            $userGroups = array();
	                            if (not_empty($userInfo['groups'])) {
	                                foreach($userInfo['groups'] as $groupId) {
	                                    $userGroup = $groupDAO->load($groupId);
	                                    if ($userGroup != null) {
	                                        $groupInfo = $userGroup->getSummary();
	                                        $groupInfo['ctime'] = format_time($groupInfo['ctime']);
	                                        $groupInfo['scoredata'] = Card::getCardsScoreSumSummary($userDAO->getUsersAndCards($groupInfo['users']));
	                                        $groupInfo['users'] = $userDAO->getUsersNames($groupInfo['users']);
	                                        $groupInfo['creator'] = (count($groupInfo['users']) > 0) ? $groupInfo['users'][0] : '';                                    
	                                        // if ($userId == $groupInfo['creator']) $groupInfo['creator'] = $userInfo['uname']; 
	                                        $userGroups[] = $groupInfo;
	                                    }                         
	                                }
	                            }
	                            $userInfo['pos'] = $userPos;
	                            $userInfo['groups'] = $userGroups;                        
	                            echo json_encode($userInfo); 
                            } else echo ':(';
                        } else echo ':(';
                    } break;
                case 'group': {
                        if (preg_match("/^\d+$/", $infoValue) > 0) {
                            $group = $groupDAO->load($infoValue);
                            $groupPos = $groupDAO->getGroupPos($infoValue);
                            if ($group != null) {                        
	                            $groupInfo = $group->getSummary();                        
	                            $groupInfo['ctime'] = format_time($groupInfo['ctime']);
	                            $groupUsers = array();
	                            if (not_empty($groupInfo['users'])) {
	                                $scoreData = Card::getCardsScoreSumSummary($userDAO->getUsersAndCards($groupInfo['users']));
	                                foreach($groupInfo['users'] as $userId) {
	                                    $groupUser = $userDAO->load($userId);
	                                    if ($groupUser != null) {
	                                        $userInfo = $groupUser->getSummary();
	                                        $userInfo['uid'] = userIdConvertor($userInfo['uid']);
	                                        $userInfo['rtime'] = format_time($userInfo['rtime']);
	                                        $userInfo['card'] = Card::getCardSummary($userInfo['card']);
	                                        $userInfo['card']['score'] = $scoreData["scores"][$userId];
	                                        if ($userId == $groupInfo['creator']) $groupInfo['creator'] = $userInfo['uname']; 
	                                        $groupUsers[] = $userInfo;
	                                    }                         
	                                }
	                                //$groupInfo['scoredata'] = $scoreData; 
	                            }
	                            $groupInfo['pos'] = $groupPos;                            
	                            $groupInfo['users'] = $groupUsers;
	                            echo json_encode($groupInfo);
                            } else echo ':(';                
                        } else echo ':(';
                    } break;
            }
        } else echo ':(';
    } else echo ':(';

?>
